function registerQBUser(email, password, login,scringoId, step){
	var TAG = "registerQBUser :: ";
	QB.createSession(function(err, result){
		if (err) {
			console.log(TAG+'createSession Something went wrong: ' + err.detail);
			connectFailed();
		} else {
			params = {full_name: login, email: email, 
					login: login, password: password,
					website: 'https://www.'+scringoId+'.com'};

			console.log(TAG+"createSession fue bien. "+result);
			QB.users.create(params, function(err, result){
				if (err) {
					alert(TAG+'Something went wrong: ' + err.detail);
					$("#signUpUserPage").spin("modal");		 

					//signUpFailed(); TODO mirar q es
				} else {					
					qbUser = result;				  
				//	$("#signUpUserPage").spin("modal");	
					//alert("Click on Menu -> Profile to change your porfile picture.");
					//show user list				 
					storage = {type: 0, email: result.email, password: password, scringoId: scringoId,qbUserId:result.id }; 
					localStorage['auth'] = $.base64.encode(JSON.stringify(storage));
					var onError = function onError(error) {
						//TODO enviar emial
					    alert('code: '    + error.code    + '\n' +
					          'message: ' + error.message + '\n');
					};
				 
					var onSuccess =function (position){												 
							createQBLocation(email,password,$("#status").val(),position,step);
					};
					navigator.geolocation.getCurrentPosition(onSuccess, onError);
					//crear location

					
					
				}
			});			 
		}
	});
}

function loginQBUser(email, password, scringoId,step){
	
	  
	QB.createSession(function(err, result){
		if (err) {
			console.log('createSession Something went wrong: ' + err.detail);			 
		} else {
			console.log("creat eSession fue bien. "+JSON.stringify(result));
			 
			params = {email: email,password: password};
			
			QB.login(params, function(err, result){
				if (err) {
					console.log('Something went wrong: ' + err.detail);					 
				} else {
					console.log("login fue bien "+JSON.stringify(result));
					
					storage = {type: 0, email: result.email, password: password, scringoId: scringoId,qbUserId:result.id }; 
					localStorage['auth'] = $.base64.encode(JSON.stringify(storage));
					
					getQBLocationByUserId(email,password,result.id,$("#status").val(),step);
				}
			}); 
			
			/*
			QB.users.listUsers(    params, function(err, result){
				if (err) {
					console.log('ListUsers Something went wrong: ' + err.detail);
					connectFailed();
				} else {
					console.log("ListUsers fue bien "+result.items);
					 
					for(var i=0; i< result.items.length;i++){
						var user = result.items[i].user;
						console.log("USer "+user.login);
					}
					//	TODO falta algo
					//xmppConnect(result.id, result.full_name, result.blob_id, login, password);
				}
			});
			*/ 
			
		}
	});
	 
}

function getQBUserPhoto(email,password,blob_id,divId,step){
	params = {email: email, password: password};
	
	QB.createSession(params, function(err, result){
		if (err) {
			console.log('Something went wrong: ' + err.detail);
		} else {
			console.log(result);
			
			QB.content.getBlobObjectById(blob_id, function(err, res){
				if (err) {
					console.log('Something went wrong: ' + JSON.stringify(err));
				} else {
					console.log('Blob object : ' + JSON.stringify(res));					
       		    	showPhotoInDiv(res.blob_object_access.params,divId);
		         	manageStep(step,res,password,"");
				}
			});
		}
	});
}

function updateQBUserPhoto(email,password,scringoId,qbUserId, file,step){
//	QB.init(QBPARAMS.app_id, QBPARAMS.auth_key, QBPARAMS.auth_secret);

	  if (file) {
	  	params = {email: email, password: password};
	  	
	  	QB.createSession(params, function(err, result){
				if (err) {
					console.log('Something went wrong: ' + err.detail);
				} else {
					console.log(result);
					console.log("file.name "+file.name);
					console.log("file.type "+file.type);

					QB.content.create({name: file.name, content_type: 'image/jpeg', 'public': true}, function(err, result){
					    if (err) {
					    	console.log('Error creating blob: ' + JSON.stringify(err));
					    	signUpFailed();
					    } else {
					      console.log(result);
					      
					      var uri = parseUri(result.blob_object_access.params);
					      var params_upload = { url: uri.protocol + '://' + uri.host };
					      var data = new FormData();
					      data.append('key', uri.queryKey.key);
					      data.append('acl', uri.queryKey.acl);
					      data.append('success_action_status', uri.queryKey.success_action_status);
					      data.append('AWSAccessKeyId', uri.queryKey.AWSAccessKeyId);
					      data.append('Policy', decodeURIComponent(uri.queryKey.Policy));
					      data.append('Signature', decodeURIComponent(uri.queryKey.Signature));
					      data.append('Content-Type', uri.queryKey['Content-Type']);
					      data.append('file', file, result.name);
					      params_upload.data = data;
					      QB.content.upload(params_upload, function(err, res){
					        if (err) {
					          console.log('Error uploading content' + err);
					        } else {
					          console.log(res);
					          var photo = JSON.stringify(res);
					          QB.content.markUploaded({id: result.id, size: file.size}, function(res){
							        console.log(res);
							        
							        QB.users.update({id: qbUserId, data: {blob_id: result.id}}, function(err, res){
							        	if (err) {
								         	console.log('Something went wrong: ' + err);
								       	} else {
								       		console.log("ficherp subido y actualizado usuario "+photo);
								       		
								       		profilePhoto = {profilePhoto : result.id}
								    		localStorage['profilePhoto'] = $.base64.encode(JSON.stringify(profilePhoto));
								    		
								       		
								       		$.each($.parseJSON(photo), function(key,value){
								       		   // alert(key+" - "+value);
								       		    if(key == "Location"){
								       		    	showPhotoInDiv(value,"previewUserPhoto");
								       		    }
								       		});
								         	//update scringo foto

								         	manageStep(step,result,password,scringoId);
								        }
								      });
							      });
					        }
					      });
					    }
				  });
				}
			});
	  }
}


function createQBLocation(email,password,status,position,step){
	var TAG = "[DEBUG] createQBLocation :: ";
	var params = {email:email,password:password}
	QB.createSession(params, function(err, result){
		if (err) {
			console.log(TAG+'createSession Something went wrong: ' + err.detail);
		} else {
			console.log(TAG+"creat eSession fue bien. "+JSON.stringify(result));			 
		    var lat = position.coords.latitude;
		    var lng = position.coords.longitude; 		    
		    params = {"latitude": lat, "longitude": lng, "status": status};
			QB.location.geodata.create(params, function(err, result){
				if (err) {
					console.log(TAG+'geodata create  went wrong: ' + err.detail);
				} else {
					console.log(TAG+"location fue bien "+JSON.stringify(result)); 
					$("#signUpUserPage").spin("modal");	
					localStorage['location'] = $.base64.encode(JSON.stringify(result));									
				}
			});
		}
	});
}


function updateQBLocation(email,password,status,position,locationId,step){
	var params = {email:email,password:password};
	console.log("[DEBUG] updateQBLocation Actualizando Location"+JSON.stringify(location));
	QB.createSession(params, function(err, result){
		if (err) {
			console.log('[DEBUG] updateQBLocation createSession Something went wrong: ' + err.detail);
		} else {
			console.log("[DEBUG] updateQBLocation create Session fue bien. "+JSON.stringify(result));			 
		    var lat = position.coords.latitude;
		    var lng = position.coords.longitude; 		    
		    params = {"latitude": lat, "longitude": lng, "status": status,"id":locationId};
			console.log("[DEBUG] updateQBLocation create Session fue bien. "+JSON.stringify(result));			 

			QB.location.geodata.update(params, function(err, result){
				if (err) {
					console.log('[DEBUG] updateQBLocation Something went wrong: ' + err.detail);
				} else {
					console.log("[DEBUG] updateQBLocation Location fue actualizado "+JSON.stringify(result)); 
					$("#signUpUserPage").spin("modal");	
					localStorage['location'] = $.base64.encode(JSON.stringify(result));									
				}
			});
		}
	});
}


function getQBLocationByUserId(email,password,userId,status,step){
	var TAG = "[DEBUG] getQBLocationByUserId :: ";
	 
	console.log(TAG+" Actualizando Location"+JSON.stringify(location));
	var params = {email:email,password:password}
	QB.createSession( function(err, result){
		if (err) {
			console.log(TAG+' createSession Something went wrong: ' + err.detail);
		} else {
			console.log(TAG+" create Session fue bien. "+JSON.stringify(result));		   
		    params = {"user.id":userId,"last_only":1};
			QB.location.geodata.list(params, function(err, result){
				if (err) {
					$("#signUpUserPage").spin("modal");	
					console.log(TAG+' geodata list went wrong: ' + err.detail);
				} else {
					console.log(TAG+" Location obtenida."); 
					 
					var onError = function onError(error) {
						//TODO enviar emial
					    alert('code: '    + error.code    + '\n' +
					          'message: ' + error.message + '\n');
					};
					var locationId= -1;
					if(typeof result.items != "undefined") {
						console.log(TAG+" Lenght "+result.items.length);
						if(typeof result.items.length != "undefined" && result.items.length>0){
							console.log(TAG+" Location obtenida "+JSON.stringify(result.items[0])); 
							locationId = result.items[0].geo_datum.id;
						}
					}
					var onSuccess =function (position){						
						if (locationId > 0 ) {						  
							updateQBLocation(email,password,status,position,locationId,step);
						}else{
							createQBLocation(email,password,status,position,step);
						}	
					};
					navigator.geolocation.getCurrentPosition(onSuccess, onError);
				}
			});
		}
	});
}
//687-11872